#include <iostream>
#include <fstream>
#include "../include/CHandleDecode.h"

CHandleDecode::CHandleDecode()
{
	this->m_fpin = nullptr;
	this->m_ublox_raw = new raw_t;
	this->m_cycle_count = 0;
}

CHandleDecode::CHandleDecode(string path, string mode)
{
	this->m_fpin = fopen(path.c_str(), mode.c_str());
	if (!this->m_fpin)
	{
		std::cerr << "File open error!" << std::endl;
	}
	this->m_ublox_raw = new raw_t;
	this->m_cycle_count = 0;
}

/* initialize receiver raw data control ----------------------------------------
* initialize receiver raw data control struct and reallocate obsevation and
* epheris buffer
* args   : raw_t  *raw   IO     receiver raw data control struct
* return : status (1:ok,0:memory allocation error)
*-----------------------------------------------------------------------------*/
void CHandleDecode::Initial()
{
	this->m_ublox_raw->nbyte = this->m_ublox_raw->len = 0;
	this->m_ublox_raw->iod = this->m_ublox_raw->flag = this->m_ublox_raw->outtype = 0;
	this->m_ublox_raw->tod = -1;
	memset(this->m_ublox_raw->buff, 0, MAXRAWLEN);
	memset(&this->m_ublox_raw->decode_pv, 0, sizeof(this->m_ublox_raw->decode_pv));
	strcpy(this->m_ublox_raw->sstr_hnr, "%10u %10hd %10hhu %10hhu %10hhu %10hhu %10hhu %10hhu %10.6lf   %10hhu %10hhu %10hhu %10hhu  %10.6lf %10.6lf %10.6lf %10.6lf %10.6lf %10.6lf %10.6lf %10.6lf ");
	strcpy(this->m_ublox_raw->sstr_att, "%10u %10hhu %10hhu %10hhu %10hhu   %10.6lf %10.6lf %10.6lf   %10.6lf %10.6lf %10.6lf ");
}

void CHandleDecode::DecodeBin()
{
	for (this->m_cycle_count = 0; this->m_cycle_count < INF; this->m_cycle_count++)
	{
		input_ubxf_bin(this->m_ublox_raw, this->m_fpin);
		if (feof(this->m_fpin)) break;
	}
}

void CHandleDecode::DecodeTxt()
{
	for (this->m_cycle_count = 0; this->m_cycle_count < INF; this->m_cycle_count++)
	{
		input_ubxf(this->m_ublox_raw, this->m_fpin);
		if (feof(this->m_fpin)) break;
	}
}

void CHandleDecode::OutputFile()
{
	std::ofstream f_out;
	f_out.open("output.txt", std::ios::out);
	//
	//	function TODO;
	//
	f_out.close();
}

CHandleDecode::~CHandleDecode()
{
	fclose(this->m_fpin);
	if (this->m_ublox_raw!=nullptr)
	{
		delete this->m_ublox_raw;
		this->m_ublox_raw = nullptr;
	}

}
